package com.cangchu.ui.activity;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import com.cangchu.R;
import com.cangchu.db.domain.DataRep;
import com.cangchu.db.domain.Goods;
import com.cangchu.util.InjectView;

/**
 * 商品详情页面
 * Created by Administrator on 2016/10/28.
 */
public class GoodPagerActivity extends BaseActivity<GoodPagerActivity> {


    public static final String OBJECT = "0";
    public static final String Model = "Model";
    public static final int PAGERESULT = 0;

    private String model;

    @InjectView(R.id.goods_code)
    private EditText goods_code;

    @InjectView(R.id.goods_name)
    private EditText goods_name;

    @InjectView(R.id.goods_unit)
    private EditText goods_unit;

    @InjectView(R.id.goods_provider)
    private EditText goods_provider;

    private Goods goods;


    @Override
    protected int getContentViewLayoutID() {
        return R.layout.page_good;
    }

    @Override
    protected void onPause() {
        super.onPause();
        DataRep.getInstance(this).save(goods);
        DataRep.getInstance(this).serializableData();
    }

    @Override
    protected void initViewsAndEvents() {
        if (getIntent() != null) {
            goods = (Goods) getIntent().getSerializableExtra(OBJECT);
            model = (String) getIntent().getSerializableExtra(Model);
            goods_code.setText(goods.getCode());
            goods_name.setText(goods.getName());
            goods_unit.setText(goods.getUnit());
            goods_provider.setText(goods.getProvider());
        } else {
            goods = new Goods();
        }

        goods_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                goods.setCode(charSequence.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        goods_name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                goods.setName(charSequence.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        goods_provider.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                goods.setProvider(charSequence.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        goods_unit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                goods.setUnit(charSequence.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });


    }
}
